<!DOCTYPE HTML>
<html lang="zh-hans" >
    <!-- Start book Python基础 -->
<!-- Start book Python基础 -->
    <head>
        <!-- head:start -->
<!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>匿名函数 | Python基础</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        <meta name="author" content="姜伟">
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="gitbook/style.css">
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-prism/prism.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="./chapter03.html" />
    
    
    <link rel="prev" href="./chapter01.html" />
    

        <!-- head:end -->
<!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
<!-- body:start -->
        
    <div class="book"
        data-level="2"
        data-chapter-title="匿名函数"
        data-filepath="chapter02.md"
        data-basepath="."
        data-revision="Sun Feb 09 2020 13:35:09 GMT+0800 (中国标准时间)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="./index.html">
                
                        <i class="fa fa-check"></i>
                        
                        学习目标
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="chapter01.html">
            
                
                    <a href="./chapter01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        递归函数
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="2" data-path="chapter02.html">
            
                
                    <a href="./chapter02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        匿名函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3" data-path="chapter03.html">
            
                
                    <a href="./chapter03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        高阶函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4" data-path="chapter04.html">
            
                
                    <a href="./chapter04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        闭包的概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5" data-path="chapter05.html">
            
                
                    <a href="./chapter05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        装饰器的使用
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本书使用 GitBook 发布
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="./" >Python基础</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h2 id="&#x533F;&#x540D;&#x51FD;&#x6570;">&#x533F;&#x540D;&#x51FD;&#x6570;</h2>
<p>&#x7528;lambda&#x5173;&#x952E;&#x8BCD;&#x80FD;&#x521B;&#x5EFA;&#x5C0F;&#x578B;&#x533F;&#x540D;&#x51FD;&#x6570;&#x3002;&#x8FD9;&#x79CD;&#x51FD;&#x6570;&#x5F97;&#x540D;&#x4E8E;&#x7701;&#x7565;&#x4E86;&#x7528;def&#x58F0;&#x660E;&#x51FD;&#x6570;&#x7684;&#x6807;&#x51C6;&#x6B65;&#x9AA4;&#x3002;</p>
<p>lambda&#x51FD;&#x6570;&#x7684;&#x8BED;&#x6CD5;<strong>&#x53EA;&#x5305;&#x542B;&#x4E00;&#x4E2A;&#x8BED;&#x53E5;</strong>&#xFF0C;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">lambda</span> &#x53C2;&#x6570;&#x5217;&#x8868;: &#x8FD0;&#x7B97;&#x8868;&#x8FBE;&#x5F0F;
</code></pre>
<p>&#x5982;&#x4E0B;&#x5B9E;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-python">sum = <span class="hljs-keyword">lambda</span> arg1, arg2: arg1 + arg2

<span class="hljs-comment"># &#x8C03;&#x7528;sum&#x51FD;&#x6570;</span>
print(<span class="hljs-string">&quot;Value of total : %d&quot;</span> % sum( <span class="hljs-number">10</span>, <span class="hljs-number">20</span> ))
print(<span class="hljs-string">&quot;Value of total : %d&quot;</span> % sum( <span class="hljs-number">20</span>, <span class="hljs-number">20</span> ))
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x5B9E;&#x4F8B;&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-python">Value of total :  <span class="hljs-number">30</span>
Value of total :  <span class="hljs-number">40</span>
</code></pre>
<p>Lambda&#x51FD;&#x6570;&#x80FD;&#x63A5;&#x6536;&#x4EFB;&#x4F55;&#x6570;&#x91CF;&#x7684;&#x53C2;&#x6570;&#x4F46;&#x53EA;&#x80FD;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x503C;</p>
<p>&#x533F;&#x540D;&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x6267;&#x884C;&#x4EFB;&#x610F;&#x8868;&#x8FBE;&#x5F0F;(&#x751A;&#x81F3;print&#x51FD;&#x6570;)&#xFF0C;&#x4F46;&#x662F;&#x4E00;&#x822C;&#x8BA4;&#x4E3A;&#x8868;&#x8FBE;&#x5F0F;&#x5E94;&#x8BE5;&#x6709;&#x4E00;&#x4E2A;&#x8BA1;&#x7B97;&#x7ED3;&#x679C;&#x4F9B;&#x8FD4;&#x56DE;&#x4F7F;&#x7528;&#x3002;</p>
<p>python&#x5728;&#x7F16;&#x5199;&#x4E00;&#x4E9B;&#x6267;&#x884C;&#x811A;&#x672C;&#x7684;&#x65F6;&#x5019;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;lambda&#xFF0C;&#x8FD9;&#x6837;&#x53EF;&#x4EE5;&#x63A5;&#x53D7;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#x7684;&#x8FC7;&#x7A0B;&#xFF0C;&#x6BD4;&#x5982;&#x5199;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x811A;&#x672C;&#x7BA1;&#x7406;&#x670D;&#x52A1;&#x5668;&#x3002;</p>
<h2 id="&#x5E94;&#x7528;&#x573A;&#x5408;">&#x5E94;&#x7528;&#x573A;&#x5408;</h2>
<h3 id="&#x51FD;&#x6570;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x4F20;&#x9012;">&#x51FD;&#x6570;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x4F20;&#x9012;</h3>
<pre><code class="lang-python"><span class="hljs-prompt">&gt;&gt;&gt; </span><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">fun</span><span class="hljs-params">(a, b, opt)</span>:</span>
<span class="hljs-prompt">... </span>    print(<span class="hljs-string">&quot;a = &quot;</span> % a)
<span class="hljs-prompt">... </span>    print(<span class="hljs-string">&quot;b = &quot;</span> % b)
<span class="hljs-prompt">... </span>    print(<span class="hljs-string">&quot;result =&quot;</span> % opt(a, b))
...
<span class="hljs-prompt">&gt;&gt;&gt; </span>add = <span class="hljs-keyword">lambda</span> x,y:x+y
<span class="hljs-prompt">&gt;&gt;&gt; </span>fun(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, add)  <span class="hljs-comment"># &#x628A; add &#x4F5C;&#x4E3A;&#x5B9E;&#x53C2;&#x4F20;&#x9012;</span>
a = <span class="hljs-number">1</span>
b = <span class="hljs-number">2</span>
result = <span class="hljs-number">3</span>
</code></pre>
<p>&#x7EC3;&#x4E60;:</p>
<p>&#x6709;&#x4E00;&#x4E2A;&#x5217;&#x8868;</p>
<pre><code class="lang-python">students = [
    {<span class="hljs-string">&apos;name&apos;</span>: <span class="hljs-string">&apos;zhangsan&apos;</span>, <span class="hljs-string">&apos;age&apos;</span>: <span class="hljs-number">18</span>, <span class="hljs-string">&apos;score&apos;</span>: <span class="hljs-number">92</span>},
    {<span class="hljs-string">&apos;name&apos;</span>: <span class="hljs-string">&apos;lisi&apos;</span>, <span class="hljs-string">&apos;age&apos;</span>: <span class="hljs-number">20</span>, <span class="hljs-string">&apos;score&apos;</span>: <span class="hljs-number">90</span>},
    {<span class="hljs-string">&apos;name&apos;</span>: <span class="hljs-string">&apos;wangwu&apos;</span>, <span class="hljs-string">&apos;age&apos;</span>: <span class="hljs-number">19</span>, <span class="hljs-string">&apos;score&apos;</span>: <span class="hljs-number">95</span>},
    {<span class="hljs-string">&apos;name&apos;</span>: <span class="hljs-string">&apos;jerry&apos;</span>, <span class="hljs-string">&apos;age&apos;</span>: <span class="hljs-number">21</span>, <span class="hljs-string">&apos;score&apos;</span>: <span class="hljs-number">98</span>},
    {<span class="hljs-string">&apos;name&apos;</span>: <span class="hljs-string">&apos;chris&apos;</span>, <span class="hljs-string">&apos;age&apos;</span>: <span class="hljs-number">17</span>, <span class="hljs-string">&apos;score&apos;</span>: <span class="hljs-number">100</span>},
]
</code></pre>
<p>&#x8981;&#x6C42;&#xFF0C;&#x5BF9;&#x4E0A;&#x8FF0;&#x5217;&#x8868;&#x91CC;&#x7684;&#x6570;&#x636E;&#x6309;&#x7167;score&#x8FDB;&#x884C;&#x5347;&#x5E8F;&#x6392;&#x5E8F;&#x3002;</p>
<p>Python&#x4E2D;&#x4F7F;&#x7528;&#x51FD;&#x6570;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x7684;&#x5185;&#x7F6E;&#x51FD;&#x6570;&#x548C;&#x7C7B;:</p>
<table>
  <tr>
    <th>&#x51FD;&#x6570;&#x540D;&#x6216;&#x7C7B;&#x540D;</th>
    <th>&#x529F;&#x80FD;</th>
    <th>&#x53C2;&#x6570;&#x63CF;&#x8FF0;</th>
  </tr>
  <tr>
    <td>sorted&#x51FD;&#x6570;</td>
    <td>&#x7528;&#x6765;&#x5C06;&#x4E00;&#x4E2A;&#x65E0;&#x5E8F;&#x5217;&#x8868;&#x8FDB;&#x884C;&#x6392;&#x5E8F;</td>
    <td>&#x51FD;&#x6570;&#x53C2;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x89C4;&#x5B9A;&#x6309;&#x7167;&#x5143;&#x7D20;&#x7684;&#x54EA;&#x4E2A;&#x5C5E;&#x6027;&#x8FDB;&#x884C;&#x6392;&#x5E8F;</td>
  </tr>
  <tr>
    <td>filter&#x7C7B;</td>
    <td>&#x7528;&#x6765;&#x8FC7;&#x6EE4;&#x4E00;&#x4E2A;&#x5217;&#x8868;&#x91CC;&#x7B26;&#x5408;&#x89C4;&#x5B9A;&#x7684;&#x6240;&#x6709;&#x5143;&#x7D20;&#xFF0C;&#x5F97;&#x5230;&#x7684;&#x7ED3;&#x679C;&#x662F;&#x4E00;&#x4E2A;&#x8FED;&#x4EE3;&#x5668;</td>
    <td>&#x51FD;&#x6570;&#x53C2;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x6307;&#x5B9A;&#x5143;&#x7D20;&#x6EE1;&#x8DB3;&#x7684;&#x8FC7;&#x6EE4;&#x6761;&#x4EF6;</td>
  </tr>
  <tr>
    <td>map&#x7C7B;</td>
    <td>&#x5C06;&#x5217;&#x8868;&#x91CC;&#x7684;&#x6BCF;&#x4E00;&#x9879;&#x6570;&#x636E;&#x90FD;&#x6267;&#x884C;&#x76F8;&#x540C;&#x7684;&#x64CD;&#x4F5C;&#xFF0C;&#x5F97;&#x5230;&#x7684;&#x7ED3;&#x679C;&#x662F;&#x4E00;&#x4E2A;&#x8FED;&#x4EE3;&#x5668;</td>
    <td>&#x51FD;&#x6570;&#x53C2;&#x6570;&#x7528;&#x6765;&#x6307;&#x5B9A;&#x5217;&#x8868;&#x91CC;&#x5143;&#x7D20;&#x6240;&#x6267;&#x884C;&#x7684;&#x64CD;&#x4F5C;</td>
  </tr>
  <tr>
    <td>reduce&#x51FD;&#x6570;</td>
    <td>&#x5BF9;&#x4E00;&#x4E2A;&#x5E8F;&#x5217;&#x8FDB;&#x884C;&#x538B;&#x7F29;&#x8FD0;&#x7B97;&#xFF0C;&#x5F97;&#x5230;&#x4E00;&#x4E2A;&#x503C;&#x3002;python3&#x4EE5;&#x540E;&#xFF0C;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x88AB;&#x79FB;&#x5230;&#x4E86;functools&#x6A21;&#x5757;</td>
    <td>&#x51FD;&#x6570;&#x53C2;&#x6570;&#x7528;&#x6765;&#x6307;&#x5B9A;&#x5143;&#x7D20;&#x6309;&#x7167;&#x54EA;&#x79CD;&#x65B9;&#x5F0F;&#x5408;&#x5E76;</td>
     </tr>
</table>
                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="./chapter01.html" class="navigation navigation-prev " aria-label="Previous page: 递归函数"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="./chapter03.html" class="navigation navigation-next " aria-label="Next page: 高阶函数"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="gitbook/app.js"></script>

    
    <script src="gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-copy-code-button/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"prism":{},"splitter":{},"copy-code-button":{},"toggle-chapters":{},"highlight":{},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
<!-- body:end -->
    </body>
    <!-- End of book Python基础 -->
<!-- End of book Python基础 -->
</html>
